/// 笔记角色信息视图对象
class NotesCharacterVO {
  /// 关联ID
  final int? id;

  /// 笔记ID
  final int? notesId;

  /// 角色名称
  final String characterName;

  /// 角色头像URL
  final String characterAvatar;

  /// 角色描述
  final String? characterDescription;

  /// 角色性格
  final String? characterPersonality;

  /// 角色场景
  final String? characterScenario;

  /// 角色首次消息
  final String? characterFirstMessage;

  /// 角色类型（主角/配角/反派等）
  final String? characterRole;

  /// 角色重要性（0-100）
  final int? characterImportance;

  /// 角色关系
  final String? relationships;

  /// 是否为新创建的角色
  final bool? isNewCreated;

  NotesCharacterVO({
    this.id,
    this.notesId,
    required this.characterName,
    required this.characterAvatar,
    this.characterDescription,
    this.characterPersonality,
    this.characterScenario,
    this.characterFirstMessage,
    this.characterRole,
    this.characterImportance,
    this.relationships,
    this.isNewCreated,
  });

  /// 从 JSON 创建对象
  factory NotesCharacterVO.fromJson(Map<String, dynamic> json) {
    return NotesCharacterVO(
      // 安全地将 id 转换为 int（支持 int 和 String）
      id: _parseIntSafely(json['id']),
      // 安全地将 notesId 转换为 int（支持 int 和 String）
      notesId: _parseIntSafely(json['notesId']),
      characterName: json['characterName'] ?? '',
      characterAvatar: json['characterAvatar'] ?? '',
      characterDescription: json['characterDescription'],
      characterPersonality: json['characterPersonality'],
      characterScenario: json['characterScenario'],
      characterFirstMessage: json['characterFirstMessage'],
      characterRole: json['characterRole'],
      // 安全地将 characterImportance 转换为 int
      characterImportance: _parseIntSafely(json['characterImportance']),
      relationships: json['relationships'],
      isNewCreated: json['isNewCreated'],
    );
  }

  /// 安全地解析 int 值（支持 int、String、null）
  static int? _parseIntSafely(dynamic value) {
    if (value == null) return null;
    if (value is int) return value;
    if (value is String) {
      return int.tryParse(value);
    }
    if (value is double) return value.toInt();
    return null;
  }

  /// 转换为 JSON
  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'notesId': notesId,
      'characterName': characterName,
      'characterAvatar': characterAvatar,
      'characterDescription': characterDescription,
      'characterPersonality': characterPersonality,
      'characterScenario': characterScenario,
      'characterFirstMessage': characterFirstMessage,
      'characterRole': characterRole,
      'characterImportance': characterImportance,
      'relationships': relationships,
      'isNewCreated': isNewCreated,
    };
  }

  /// 转换为 STCharacter（用于兼容现有代码）
  STCharacter toSTCharacter() {
    return STCharacter(
      name: characterName,
      avatar: characterAvatar,
      description: characterDescription,
      personality: characterPersonality,
      firstMes: characterFirstMessage,
      scenario: characterScenario,
      creatorNotes: '角色类型: $characterRole\n重要性: $characterImportance\n关系: $relationships',
      tags: [
        if (characterRole != null) characterRole!,
        'notes_character',
        if (isNewCreated == true) 'new_created',
      ],
    );
  }

  @override
  String toString() {
    return 'NotesCharacterVO(name: $characterName, role: $characterRole, importance: $characterImportance)';
  }
}

/// SillyTavern 角色模型（保持兼容）
class STCharacter {
  final String name;
  final String avatar;
  final String? description;
  final String? personality;
  final String? firstMes;
  final String? scenario;
  final String? creatorNotes;
  final List<String>? tags;

  STCharacter({
    required this.name,
    required this.avatar,
    this.description,
    this.personality,
    this.firstMes,
    this.scenario,
    this.creatorNotes,
    this.tags,
  });

  factory STCharacter.fromJson(Map<String, dynamic> json) {
    return STCharacter(
      name: json['name'] ?? '',
      avatar: json['avatar'] ?? '',
      description: json['description'],
      personality: json['personality'],
      firstMes: json['first_mes'],
      scenario: json['scenario'],
      creatorNotes: json['creator_notes'],
      tags: json['tags'] != null ? List<String>.from(json['tags']) : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'name': name,
      'avatar': avatar,
      'description': description,
      'personality': personality,
      'first_mes': firstMes,
      'scenario': scenario,
      'creator_notes': creatorNotes,
      'tags': tags,
    };
  }
}

